package com.example.firstjavamqtt.controller.mqtt_api;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.firstjavamqtt.common.excepiton.GlobalException;
import com.example.firstjavamqtt.common.result.Result;
import com.example.firstjavamqtt.common.result.ResultCodeEnum;
import com.example.firstjavamqtt.model.base.BaseDeviceLogDto;
import com.example.firstjavamqtt.model.dto.mqtt.MqttInfraredDto;
import com.example.firstjavamqtt.model.dto.mqtt.MqttTemperatureHumidityDto;
import com.example.firstjavamqtt.model.log.InfraredMonitorLog;
import com.example.firstjavamqtt.model.log.TemperatureHumidityMonitorLog;
import com.example.firstjavamqtt.service.log.DeviceLogService;
import com.example.firstjavamqtt.service.log.InfraredLogService;
import com.example.firstjavamqtt.service.log.TemperatureHumidityLogService;
import com.example.firstjavamqtt.utils.BeanListUtils;
import com.example.firstjavamqtt.utils.RedisUtil;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/mqtt/infrared")
@Api(tags = "红外人体检测接口")
public class InfraredController {

    @Autowired
    RedisUtil redisUtil;

    @Autowired
    InfraredLogService infraredLogService;

    @Autowired
    DeviceLogService deviceLogService;


    @ApiOperation("刷新redis 获取红外消息列表")
    @GetMapping(value = "/getInfrared/{pageNum}/{limit}")
    public Result getInfraredList(@PathVariable int pageNum,
                                  @PathVariable int limit,
                                  BaseDeviceLogDto dto
                                  ) {
        //先把redis的数据存到数据库中
        infraredLogService.saveByDeviceId(dto.getDeviceId());
        deviceLogService.saveInfraredByDeviceId(dto.getDeviceId());
        //存完数据库就删除
        redisUtil.del("Infrared:" + dto.getDeviceId());
        //从这开始 真正查询数据库并分页返回
        Page<InfraredMonitorLog> pageModel = infraredLogService.getInfraredListByDto(pageNum,limit,dto);
        return Result.ok(pageModel);
    }

}
